//
//  LoginViewController.m
//  图片故事
//
//  Created by 熊坤 on 16/3/1.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "Header.h"
#import <BmobSDK/Bmob.h>
#import <BmobSDK/BmobQuery.h>
#import <BmobSDK/BmobProFile.h>
#import "ForgetViewController.h"
#import "MBProgressHUD+PKX.h"
#import "UserInfoModal.h"
#import "LocaData.h"

@interface LoginViewController ()
@property (nonatomic,strong)UIButton *Button;
@property (nonatomic,strong)UITextField *NumberTextField;
@property (nonatomic,strong)UITextField *PasswordTextField;
@property (nonatomic,strong)UIButton *RegistrationButton;
@property (nonatomic,strong)UIButton *HelpButton;
@property (nonatomic,strong)UIImageView *PortraitImageView;

@end

@implementation LoginViewController

#pragma mark --懒加载
//登陆
-(UIButton *)Button {
    if (!_Button) {
        _Button = [UIButton buttonWithType:UIButtonTypeCustom];
        _Button.frame = CGRectMake(My_Width(20), My_Height(380), SCREEN_W - My_Width(40), My_Height(50));
        [_Button setTitle:@"登陆" forState:UIControlStateNormal];
        _Button.backgroundColor = [UIColor colorWithRed:102 / 255.0 green:214 / 255.0 blue:228 / 255.0 alpha:1];
        _Button.titleLabel.font = [UIFont systemFontOfSize:16.0];
        [_Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _Button.layer.cornerRadius = 5;
        
        [_Button addTarget:self action:@selector(loginClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _Button;
}

//账号
-(UITextField *)NumberTextField {
    if (!_NumberTextField) {
        _NumberTextField = [[UITextField alloc] initWithFrame:CGRectMake(My_Width(0), My_Height(250), SCREEN_W, My_Height(50))];
        _NumberTextField.placeholder = @"手机号";
        _NumberTextField.font =[UIFont systemFontOfSize:16.0];
        _NumberTextField.backgroundColor = Btn_Color;
        [_NumberTextField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
        _NumberTextField.clearButtonMode = UITextFieldViewModeWhileEditing;//右边清楚按钮模式
        _NumberTextField.textAlignment = NSTextAlignmentCenter;
    }
    return _NumberTextField;
}
//密码
-(UITextField *)PasswordTextField {
    if (!_PasswordTextField) {
        _PasswordTextField = [[UITextField alloc] initWithFrame:CGRectMake(My_Width(0), My_Height(301), SCREEN_W, My_Height(50))];
        _PasswordTextField.placeholder= @"密码";
        _PasswordTextField.font = [UIFont systemFontOfSize:16.0];
        _PasswordTextField.backgroundColor = Btn_Color;
        _PasswordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _PasswordTextField.textAlignment = NSTextAlignmentCenter;
        [_PasswordTextField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
        _PasswordTextField.secureTextEntry = YES;
    }
    return _PasswordTextField;
}
//注册
-(UIButton *)RegistrationButton {
    if (!_RegistrationButton) {
        _RegistrationButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _RegistrationButton.frame = CGRectMake(My_Width(290), My_Height(620), My_Width(70), My_Height(30));
        [_RegistrationButton setTitle:@"新用户" forState:UIControlStateNormal];
        _RegistrationButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        [_RegistrationButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_RegistrationButton addTarget:self action:@selector(registerbutton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _RegistrationButton;
}
//帮助
-(UIButton *)HelpButton {
    if (!_HelpButton) {
        _HelpButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _HelpButton.frame = CGRectMake(My_Width(20), My_Height(620), My_Width(80), My_Height(30));
        [_HelpButton setTitle:@"无法登陆" forState:UIControlStateNormal];
        _HelpButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        [_HelpButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_HelpButton addTarget:self action:@selector(btnClieked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _HelpButton;
}
//头像
-(UIImageView *)PortraitImageView {
    if (!_PortraitImageView) {
        
        _PortraitImageView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_W - My_Width(200)) / 2, My_Height(115), My_Width(200), My_Height(100))];
        
        _PortraitImageView.image = [UIImage imageNamed:@"login_logo"];
        
        _PortraitImageView.layer.masksToBounds = YES;
    
    }
    return _PortraitImageView;
}

#pragma mark --跳转
-(void)registerbutton {
    RegisterViewController *RE = [[RegisterViewController alloc] init];
    RE.identifier = @"regist";
    [self.navigationController pushViewController:RE animated:YES];
    
}

- (void) btnClieked {
    
    RegisterViewController *forget = [[RegisterViewController alloc] init];
    forget.identifier = @"forget";
    [self.navigationController pushViewController:forget animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = Bg_Color;
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(returnbutton)];
    
    self.navigationItem.leftBarButtonItem = backItem;

    
    [self.view addSubview:self.Button];
    [self.view addSubview:self.NumberTextField];
    [self.view addSubview:self.PasswordTextField];
    [self.view addSubview:self.RegistrationButton];
    [self.view addSubview:self.HelpButton];
    [self.view addSubview:self.PortraitImageView];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_hideKeyBoard)];
    
    [self.view addGestureRecognizer:tapGesture];
}

#pragma mark -- 点击事件
- (void) action_hideKeyBoard {

    [self.view endEditing:NO];
}

- (void)returnbutton {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)loginClicked {
    
    NSString *nameStr = self.NumberTextField.text;
    NSString *pwdStr = self.PasswordTextField.text;
    
    if (nameStr == nil || pwdStr == nil || nameStr.length == 0 || pwdStr.length == 0) {
        
        [MBProgressHUD showError:@"密码或者帐号不能为空"];
        return;
    }
    
    NSString *sql = [NSString stringWithFormat:@"select * from user where userName = '%@' and userPassword = '%@'",nameStr,pwdStr];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        BmobQuery *query = [[BmobQuery alloc] init];
        
        [query queryInBackgroundWithBQL:sql block:^(BQLQueryResult *result, NSError *error) {
            if (error) {
                
                [MBProgressHUD showError:@"未知错误"];
            } else if (result.resultsAry.count == 0){
                [MBProgressHUD showError:@"账号或密码错误"];
            } else {

                if (result.resultsAry.count > 0) {
                    
                    NSDictionary *dict1 = result.resultsAry[0];
                    
                    UserInfoModal *userInfo = [[UserInfoModal alloc] init];
                    
                    [userInfo setUserHead:[dict1 objectForKey:@"titleImage"]];
                    [userInfo setUserId:[dict1 objectForKey:@"objectId"]];
                    [userInfo setUserName:[dict1 objectForKey:@"userName"]];
                    [userInfo setUserPwd:[dict1 objectForKey:@"userPassword"]];
                    
                    [LocaData saveUserInfo:userInfo];
                }
                
                // 主线程更新界面
                dispatch_async(dispatch_get_main_queue(), ^{
                    [MBProgressHUD showSuccess:@"登陆成功"];
                    [self.navigationController popToRootViewControllerAnimated:YES];
                });
            }
            
        }];
    });
}

@end
